function swap(array, idx1, idx2) {
	var temp = array[idx1];
	array[idx1] = array[idx2];
	array[idx2] = temp;
}

var RouteTable = {
	routes: new Array(),
	onUpdate: undefined
};

RouteTable.addRoute = function(from, to) {
	from = from.replace(/^\s+|\s+$/g, '');
	to = to.replace(/^\s+|\s+$/g, '');
	
	var route = {
		from: from,
		to: to
	};
	
	RouteTable.routes.push(route);
	RouteTable.onUpdate();
};

RouteTable.moveUp = function(key) {
	if (key > 0) {
		swap(RouteTable.routes, key -1, key);
		RouteTable.onUpdate();
	}
}

RouteTable.moveDown = function(key) {
	if (key < RouteTable.routes.length -1) {
		swap(RouteTable.routes, key +1, key);
		RouteTable.onUpdate();
	}
}

RouteTable.deleteRoute = function(key) {
	RouteTable.routes.splice(key, 1);
	RouteTable.onUpdate();
}

RouteTable.toHtml = function() {
	var html = '';
	
	html += '<table class="table table-bordered table-condensed">';
	html += '<thead>';
	html += '<tr><th>?</th><th>forward to @jabber</th><th></th></tr>';
	html += '</thead>';
	html += '<tbody>';
	
	for (key in RouteTable.routes) {
		var route = RouteTable.routes[key];
		html += '<tr><td>' + route.from + '</td><td>' + route.to + '</td><td>';
		// html += '<a href="javascript: RouteTable.moveUp('+key+');"><i class="icon-arrow-up" /></a>';
		// html += '<a href="javascript: RouteTable.moveDown('+key+');"><i class="icon-arrow-down" /></a>';
		html += '<a href="javascript: RouteTable.deleteRoute('+key+');"><i class="icon-trash" /></a>';
		html += '</td></tr>';
	}
	
	//default route
	html += '<tr><td><strong>*</strong></td><td>&#10006;</td><td></tr></tr>';
	
	html += '</tbody>';
	html += '</table>';
	
	return html;
};

 
